package com.vooda.business_admin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vooda.business_admin.common.ReqDataTableDto;
import com.vooda.business_admin.common.RspDataTableDto;
import com.vooda.business_admin.config.CdnProperties;
import com.vooda.business_admin.dao.HomePageMapper;
import com.vooda.business_admin.entity.HomePage;
import com.vooda.business_admin.service.HomePageService;
import com.vooda.business_admin.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(readOnly = true)
public class HomePageServiceImpl implements HomePageService{

    @Autowired
    private HomePageMapper homePageMapper;

    @Autowired
    private CdnProperties cdnProperties;

    @Override
    @Transactional(readOnly = false)
    public int deleteByPrimaryKey(Long id) {
        return homePageMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public int insertSelective(HomePage record) {
        Long maxId = homePageMapper.findMaxId();
        record.setId(maxId+1);
        record.setCreateTime(new Date());
        record.setModifiedTime(new Date());
        return homePageMapper.insertSelective(record);
    }

    @Override
    public HomePage selectByPrimaryKey(Long id) {
        return homePageMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateByPrimaryKeySelective(HomePage record) {
        record.setModifiedTime(new Date());
        return homePageMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public RspDataTableDto<HomePage> findHomePageList(ReqDataTableDto reqDataTableDto,String status) {
        PageHelper.startPage(reqDataTableDto.getPage()+1,reqDataTableDto.getLength());
        List<HomePage> list = homePageMapper.findAllList(StringUtils.isBlank(status)?null:Integer.parseInt(status));
        PageInfo<HomePage> pageInfo =  new PageInfo<HomePage>(list);
        return new RspDataTableDto<HomePage>(reqDataTableDto.getDraw(),pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    @Transactional(readOnly = false)
    public String saveHomePage(HomePage homePage, MultipartFile image) {
        if (!ObjectUtils.isEmpty(image)) {
            //上传封面图片
            MultipartFile file = image;
            File f = new File(cdnProperties.getBasePath());
            if (!f.exists()) {
                f.mkdirs();
            }

            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));

            File newFile = new File(f.getAbsolutePath(), newName);
            try {
                newFile.createNewFile();
                file.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            homePage.setImageUrl(newName);
        }
        return insertSelective(homePage) > 0?"success":"fail";
    }

    @Override
    @Transactional(readOnly = false)
    public void updateImage(MultipartFile imageUpload, Long id) {
        File f = new File(cdnProperties.getBasePath());
        if (!f.exists()) {
            f.mkdirs();
        }
        String oldName = imageUpload.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        File newFile = new File(f.getAbsolutePath(), newName);
        try {
            newFile.createNewFile();
            imageUpload.transferTo(newFile);
            imageUpload.getInputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HomePage homePage = selectByPrimaryKey(id);
        homePage.setImageUrl(newName);
        homePage.setModifiedTime(new Date());
        updateByPrimaryKeySelective(homePage);
    }

    @Override
    @Transactional(readOnly = false)
    public void modifyStatus(Long id) {
        HomePage homePage = selectByPrimaryKey(id);
        homePage.setStatus(homePage.getStatus()?false:true);
        updateByPrimaryKeySelective(homePage);
    }
}
